//
//  StatusListViewModel.swift
//  weibo
//
//  Created by apple on 15/12/21.
//  Copyright © 2015年 LYR. All rights reserved.
//

import UIKit
import SVProgressHUD

class StatusListViewModel: NSObject {

    class func loadHomePageData(since_id: Int64,max_id: Int64,finished: (statuses: [Status]?) -> ()) {
        
        //实现网络请求
        //  get
        //  URL
        let url = "2/statuses/home_timeline.json"
        
        guard let token = UserAccountViewModel().access_token else {
            
            print("token 为空")
            
            SVProgressHUD.showInfoWithStatus("请重新登录")
            
            return
        }
        
        var prameters = ["access_token" : token]
         //判断since_id 是否大于0
        if since_id > 0 {
           prameters["since_id"] = "\(since_id)"
        }
        
        if max_id > 0 {
            prameters["max_id"] = "\(max_id - 1)"
        }
        
        NetworkTools.sharedTools.requestJSONDict(.GET, urlString: url, parameters: prameters) { (dict, error) -> () in
            
            if error != nil {
            // 执行失败的回调
               SVProgressHUD.showInfoWithStatus("网络有问题，请重新登录")
               return
            }
            
            //获取数据成功
            //  statuses  就是要获取的微博内容  定义一个数组来接收
            if let array = dict!["statuses"] as? [[String : AnyObject]] {
                
                //定义一个  status 模型数组
                var list = [Status]()
                for item in array {
                    
                    let s = Status(dict: item)
                    list.append(s)
                    
                }
                // 执行成功的回调
                finished(statuses: list)
             }
        
        }
        
    }
}
